package com.yangye.niodemo.netty.self;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.SocketChannel;


/**
 * @author yangye
 * @date 2020/12/24
 */
public class MyAcceptHandler extends ChannelInboundHandlerAdapter {

	private EventLoopGroup selector;
	private ChannelHandler handler;

	public MyAcceptHandler(EventLoopGroup selector, ChannelHandler handler) {
		this.selector = selector;
		this.handler = handler;
	}

	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
		System.out.println("server registered...");
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		// 有客户端连接过来
		SocketChannel client = (SocketChannel) msg;
		ChannelPipeline p = client.pipeline();
		p.addLast(handler);

		// 注册到多路复用器上
		selector.register(client);
	}
}
